package lcr;

public class Test0053 {
}


class Solution0053_1 {
    TreeNode ans;
    boolean flag;
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        dfs(root, p);
        return ans;
    }

    public void dfs(TreeNode node, TreeNode p){
        if (node==null) return;
        dfs(node.left, p);
        if (ans!=null) return;
        if (flag&&node.val> p.val) ans = node;
        if (node==p) flag = true;
        dfs(node.right, p);
    }
}